package project.cti.excel.util;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;

/**
 * 
 *
 *
 * VERSION        NAME        DATE        REASON
 * ----------------------------------------------------------------------------
 * 1.00           Brian       Feb 11, 2012  Create
 * @author brian
 * @version 1.00
 */
public class ExcelUtil {

    /**
     * 读取excel文件。按照表单，列名读取所有行。<BR>
     * 
     * @param filePath 文件路径
     * @param sheetName 要读取的表单名
     * @param rowIndex 那一行为参考行(比较列名)
     * @param cellNames 需要读取的列名
     */
    public static void readExcel(String filePath, String sheetName, int rowIndex, String... cellNames) {

        Map<String, Integer> colume = new HashMap<String, Integer>();
        try {
            // 根据excel文件路径创建文件流
            FileInputStream fis = new FileInputStream(filePath);
            // 利用poi读取excel文件流
            POIFSFileSystem fs = new POIFSFileSystem(fis);
            // 读取excel工作簿
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            // 得到sheet
            HSSFSheet sheet = wb.getSheet(sheetName);
            // 取得参考行
            HSSFRow row = sheet.getRow(rowIndex);
            if (row != null) {
                // 获取该行中总共有多少列数据row.getLastCellNum()
                for (int i = 0; i < row.getLastCellNum(); i++) {
                 // 获取该行中的一个单元格对象
                    HSSFCell cell = row.getCell(i); 
                    // 判断单元格数据类型
                    if (cell != null) {
                        // 拿到要取的列
                        int type = cell.getCellType();
                        if (type == Cell.CELL_TYPE_NUMERIC) {
                            Double value = cell.getNumericCellValue();
                            if (isContain(value.toString(), cellNames)) {
                                colume.put(value.toString(), cell.getColumnIndex());
                            }
                        } else {
                            String value = cell.getStringCellValue();
                            if (isContain(value.toString(), cellNames)) {
                                colume.put(value, cell.getColumnIndex());
                            }
                        }
                    }
                }
            }
            // 获取sheet中总共有多少行数据
            for (int i = rowIndex + 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                // 取出sheet中的某一行数据
                HSSFRow rowTemp = sheet.getRow(i);
                for (String colum : colume.keySet()) {
                    Integer index = colume.get(colum);
                    Cell cell = rowTemp.getCell(index);
                    int type = cell.getCellType();
                    // 取出想要的数据
                    if (type == Cell.CELL_TYPE_NUMERIC) {
                        Double value = cell.getNumericCellValue();
                        System.out.println(colum + " : " + value);
                    } else {
                        String value = cell.getStringCellValue();
                        System.out.println(colum + " : " + value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Excel read error!");
        }
    }

    private static boolean isContain(String value, String... values) {

        for (String v : values) {
            if (v.equals(value)) {
                return true;
            }
        }
        return false;
    }

}
